import notification from "@/services/notification";
import { observable, action,makeObservable } from 'mobx';
import Project from "@/services/project";
import ProjectModel from "@/store/models/ProjectModel";
import GlobalFunctions from '@/store/globalFunctions';
import Workflow from "@/services/workflow";
import CheckDialog from "@/components/workflow/CheckDialog";
import { currentUser } from "@/services/auth";
import Enums from "@/services/enums";
import moment from "moment";

class ProjectStore {
  @observable projectModel = new ProjectModel(0);
  constructor() {
    makeObservable(this)
  }
  @action initStore(id) {
    this.init(id);
  }
  init(id) {
    if (id !== "0" && id !== 0) {
      Project.get({ id: id }).then(data => {
        this.projectModel = ProjectModel.fromJS(data);
        Workflow.edit({ bill: "project", billtype: 0, billid: id, userid: currentUser.id }).then(data1 => {
          if (data1.length > 0) {
            this.projectModel.setBilledit(data1[0]);
          }
        }).catch(error => {
        });
        
      }).catch(error => {
        this.projectModel = new ProjectModel(0);
        this.getcode("project",moment(),0,"code");
      });
    } else {
      Project.getid({ id: 0 }).then(data => {
        this.projectModel = new ProjectModel(data[0].id);
        this.getcode("project",moment(),0,"code");
      });
    }
  }
  getcode(tblname,bill_date,type,tblfield) {
    Enums.getcode( {tblname: tblname,bill_date: bill_date,type: type,tblfield: tblfield,}).then(data => {
      this.projectModel.setCode(data[0].code);
    }).catch(error => {
      notification.error(error.message, error.detailedMessage);
    });
  };

  dataSave() {
    let certificate = [];
    if (this.projectModel.certificate1.length > 0) {
      certificate.push(this.projectModel.certificate1.join('-'))
    }
    if (this.projectModel.certificate2.length > 0) {
      certificate.push(this.projectModel.certificate2.join('-'))
    }
    if (this.projectModel.certificate3.length > 0) {
      certificate.push(this.projectModel.certificate3.join('-'))
    }
    if (this.projectModel.certificate4.length > 0) {
      certificate.push(this.projectModel.certificate4.join('-'))
    }
    if (this.projectModel.certificate5.length > 0) {
      certificate.push(this.projectModel.certificate5.join('-'))
    }
    if (this.projectModel.certificate6.length > 0) {
      certificate.push(this.projectModel.certificate6.join('-'))
    }

    if (GlobalFunctions.isEmptyobj(this.projectModel.bill_date)) {
      notification.error("日期不能为空");
      return;
    }
    if (GlobalFunctions.isEmptyobj(this.projectModel.code)) {
      notification.error("编号不能为空");
      return;
    }

    if (GlobalFunctions.isEmptyobj(this.projectModel.customerid)) {
      notification.error("客户不能为空");
      return;
    } else if (this.projectModel.customerid === 0) {
      notification.error("客户不能为空");
      return;
    }
    if (GlobalFunctions.isEmptyobj(this.projectModel.amount)) {
      notification.error("挂靠费用不能为空");
      return;
    } else if (this.projectModel.amount === 0) {
      notification.error("挂靠费用不能为空");
      return;
    }

    if (this.projectModel.bytstatus === "已签") {
      if (GlobalFunctions.isEmptyobj(this.projectModel.signingtime)) {
        notification.error("已签的项目，签约时间不能为空");
        return;
      }
      if (GlobalFunctions.isEmptyobj(this.projectModel.expirationtime)) {
        notification.error("已签的项目，到期时间不能为空");
        return;
      }
    }
    if (certificate.length < 1) {
      notification.error("需求证书不能为空");
      return;
    }
    let data = {
      id: this.projectModel.id,//
      bill_date: this.projectModel.bill_date,//--申请日期
      code: this.projectModel.code,//--编码
      name: this.projectModel.name,//名称
      customerid: this.projectModel.customerid,//客户ID
      neednum: this.projectModel.neednum,//需求数量
      amount: this.projectModel.amount,//挂靠费用
      major: this.projectModel.major,//需求专业
      schooled: this.projectModel.schooled,//学历要求
      certificate: certificate,//需求证书
      location: this.projectModel.location,//挂靠地点
      socialsecurity: this.projectModel.socialsecurity,//社保情况（无，有）
      area: this.projectModel.area,//社保地区
      bytstatus: this.projectModel.bytstatus,//敲定、 已签、带证报价
      periodunit: this.projectModel.periodunit,//期限单位
      periods: this.projectModel.periods,//期限
      signingtime: this.projectModel.signingtime,//签约时间
      expirationtime: this.projectModel.expirationtime,//到期时间
      status: this.projectModel.status,//审核状态:0 待审核、1 审核中、2 已审核
      purpose: this.projectModel.purpose,//证书用途
      certificatestatus: this.projectModel.certificatestatus,//证书状态 不限，初始注册，转注册
      locationneed: this.projectModel.locationneed,//证书地区要求 不限，附近省份，省内
      message: this.projectModel.message,//备注
      signatory: this.projectModel.signatory,//签约人
      userid: this.projectModel.userid,//录入人ID
      orgunitid: this.projectModel.orgunitid,//部门ID
      contractcode: this.projectModel.contractcode,//合同编码
      threeperson: this.projectModel.threeperson,
    }
    if (this.projectModel.isnew) {
      Project.create(data)
        .then(() => {
          this.init(this.projectModel.id);
          notification.success("新增成功.");
        })
        .catch(error => {
          notification.error("保存失败", error.response.data.message);
        });
    } else {
      Project.save(data)
        .then(() => {
          notification.success("修改成功.");
        })
        .catch(error => {
          notification.error("保存失败", error.response.data.message);
        });
    }
  };
  dataDelete() {
    Project.delete({ id: this.projectModel.id }).then(() => {
      notification.success("删除成功.");
      this.init(0);
    }).catch(error => {
      notification.error("删除失败", error.response.data.message);
    });
  };
  billCheck(userid) {
    let item = {
      bill: "project",
      billtype: 0,
      billid: this.projectModel.id,
      next_type: 1,
      message: '<div>同意</div>',
      userid: userid,
      billdate: this.projectModel.bill_date.format('YYYY-MM-DD'),
      orgunitid: this.projectModel.orgunitid,
      status: this.projectModel.status,
    }
    if (this.projectModel.status === 0) {

      Workflow.check(item).then(data => {
        if (data[0].flag === 0) {
          this.initStore(this.projectModel.id);

          notification.success(data[0].message);
        } else {
          notification.error(data[0].message);
        }
      })
        .catch(error => {
          notification.error(error.message, error.detailedMessage);
        });
    } else {
      CheckDialog.showModal({ bill_check: item })
        .onClose((result = {}) => {
          if (result.success) {
            this.initStore(this.projectModel.id);
          }
        }).onDismiss(() => {
        });
    }
  };
}

export default ProjectStore;